package Server;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Scanner;

import Shared.Doctor;
import Shared.Government;
import Shared.Nurse;
import Shared.Patient;
import Shared.User;

public class ServerPrinter extends Thread {
	private Database DB;
	private Log log;
	private SecureRandom random;
	private MessageDigest md;

	public ServerPrinter(Database DB, Log log) {
		this.DB = DB;
		this.log = log;
		random = new SecureRandom();
		try {
			md = MessageDigest.getInstance("SHA-512");
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	public void run() {
		@SuppressWarnings("resource")
		Scanner scan = new Scanner(System.in);
		String read;
		while (true) {
			System.out.println("Would you like to:");
			System.out.println("0: add a new user");
			System.out.println("1: print the log");
			System.out.println("2: list user db");
			read = scan.nextLine();

			if (Integer.valueOf(read).equals(0)) {
				System.out.println("User type?");
				String type = scan.nextLine();

				System.out.println("User division?");
				String division = scan.nextLine();

				System.out.println("User name?");
				String name = scan.nextLine();

				System.out.println("User password");
				String password = scan.nextLine();
				String salt = salt();
				try {
					User u = null;
					
					if (type.toLowerCase().equals("doctor")) {
						System.out.println("What is the name of the patient");
						String treating = scan.nextLine();
						
						u = new Doctor(division, name, hash(password,
								salt), treating, salt);
					} else if (type.toLowerCase().equals("nurse")) {
						u = new Nurse(division, name, hash(password,
								salt), salt);
					} else if (type.toLowerCase().equals("patient")) {
						u = new Patient(division, name, hash(password,
								salt), salt);
					} else if (type.toLowerCase().equals("government")) {
						u = new Government(division, name, hash(
								password, salt), salt);
					} else {
						System.out.println("Make sure all fields are correct");
						continue;
					}
					
					DB.addUser(u);
					System.out.println("User " + u.name + " saved!");
					
				} catch (Exception e) {
					System.out.println("You did something the wrong way");
				}

			} else if (Integer.valueOf(read).equals(1)) {
				log.printLog();
			} else if (Integer.valueOf(read).equals(2)) {
				DB.listUsers();
			} else {
				System.out.println("Invalid input!");
			}
		}
	}

	private String hash(String password, String salt) {
		byte[] hash = md.digest((salt + password).getBytes());
		StringBuilder sb = new StringBuilder();
		for(byte b : hash){
			sb.append(b);
		}
		return sb.toString();
	}

	private String salt() {
		return new BigInteger(130, random).toString(32);
	}
}
